//
//  ReportWithChartView.m
//  BabyHelper
//
//  Created by  thomas on 11-2-13.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ReportWithChartView.h"


@implementation ReportWithChartView
@synthesize categoryControl,countChartView,amountChartView;
@synthesize XAxisData;
@synthesize countEatMumYAxisData;
@synthesize amountEatMumYAxisData;
@synthesize countEatMilkYAxisData;
@synthesize amountEatMilkYAxisData;
@synthesize countDrinkWaterYAxisData;
@synthesize amountDrinkWaterYAxisData;
@synthesize countPeeYAxisData;
@synthesize countShitYAxisData;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	self.title = NSLocalizedString(@"Last 5 days", @"");
	
	[self.categoryControl setTitle:NSLocalizedString(@"Mum",@"")     forSegmentAtIndex:0];
	[self.categoryControl setTitle:NSLocalizedString(@"Milk",@"")    forSegmentAtIndex:1];
	[self.categoryControl setTitle:NSLocalizedString(@"Water",@"") forSegmentAtIndex:2];
	[self.categoryControl setTitle:NSLocalizedString(@"Pee",@"")         forSegmentAtIndex:3];
	[self.categoryControl setTitle:NSLocalizedString(@"Shit",@"")        forSegmentAtIndex:4];
    [super viewDidLoad];
	
	self.XAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.countEatMumYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.amountEatMumYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.countEatMilkYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.amountEatMilkYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.countDrinkWaterYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.amountDrinkWaterYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.countPeeYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	self.countShitYAxisData = [[NSMutableArray alloc] initWithCapacity:5];
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
}

-(void)viewWillAppear:(BOOL)animated{
	[self.XAxisData removeAllObjects];
	[self.countEatMumYAxisData removeAllObjects];
	[self.amountEatMumYAxisData removeAllObjects];
	[self.countEatMilkYAxisData removeAllObjects];
	[self.amountEatMilkYAxisData removeAllObjects];
	[self.countDrinkWaterYAxisData removeAllObjects];
	[self.amountDrinkWaterYAxisData removeAllObjects];
	[self.countPeeYAxisData removeAllObjects];
	[self.countShitYAxisData removeAllObjects];
	
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:DATE_FORMAT];
	NSString *strTime = [dateFormatter stringFromDate:[NSDate date]];
    NSDate *today = [dateFormatter dateFromString:strTime];
	[dateFormatter setDateFormat:@"MM/dd"];
	
	Dao *dao = [Dao getInstance];
	NSTimeInterval secondsIn24Hours = 24 * 60 * 60;
	for (int i=0; i<5; i++){
		
		NSString *strTime = [dateFormatter stringFromDate:today];	
		[self.XAxisData addObject:strTime];
		NSLog(@"%@",strTime);
		
		BabyActivityReportEntry *dayReport = [[BabyActivityReportEntry alloc]init];
		[dao GetDayReport:today report:dayReport];
		[self.countEatMumYAxisData addObject:[NSNumber numberWithInt:dayReport.countOfEatMum]];
		[self.amountEatMumYAxisData addObject:[NSNumber numberWithInt:dayReport.amountOfEatMum]]; 
		[self.countEatMilkYAxisData addObject:[NSNumber numberWithInt:dayReport.countOfEatMilk]];
		[self.amountEatMilkYAxisData addObject:[NSNumber numberWithInt:dayReport.amountOfEatMilk]];
		[self.countDrinkWaterYAxisData addObject:[NSNumber numberWithInt:dayReport.countOfDrinkWater]];
		[self.amountDrinkWaterYAxisData addObject:[NSNumber numberWithInt:dayReport.amountOfDrinkWater]];
		[self.countPeeYAxisData addObject:[NSNumber numberWithInt:dayReport.countOfPee]];
		[self.countShitYAxisData addObject:[NSNumber numberWithInt:dayReport.countOfShit]];
		[dayReport release];
		today = [today addTimeInterval:-secondsIn24Hours];
	}
	[dateFormatter release];	 
	
	self.countChartView.xData  = self.XAxisData;
	self.amountChartView.xData = self.XAxisData;
	self.countChartView.yData  = [NSArray arrayWithObject:self.countEatMumYAxisData];
	self.amountChartView.yData = [NSArray arrayWithObject:self.amountEatMumYAxisData];
	self.countChartView.ySuffix = NSLocalizedString(@"time", @"");
	self.amountChartView.ySuffix= @"min";
	self.amountChartView.hidden = false;
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	self.categoryControl = nil;
	self.countChartView  = nil;
	self.amountChartView = nil;
	self.XAxisData = nil;
	self.countEatMumYAxisData  = nil;
	self.amountEatMumYAxisData = nil;
	self.countEatMilkYAxisData = nil;
	self.amountEatMilkYAxisData= nil;
	self.countDrinkWaterYAxisData= nil;;
	self.amountDrinkWaterYAxisData= nil;;
	self.countPeeYAxisData= nil;
	self.countShitYAxisData= nil;
}


- (void)dealloc {
	[self.XAxisData release];
	[self.countEatMumYAxisData  release];
	[self.amountEatMumYAxisData release];
	[self.countEatMilkYAxisData release];
	[self.amountEatMilkYAxisData release];
	[self.countDrinkWaterYAxisData release];
	[self.amountDrinkWaterYAxisData release];
	[self.countPeeYAxisData release];
	[self.countShitYAxisData release];
	[super dealloc];
}


-(IBAction)toggleControls:(id)sender{
	self.countChartView.xData  = self.XAxisData;
	self.amountChartView.xData = self.XAxisData;
	self.countChartView.ySuffix= NSLocalizedString(@"time", @"");
	
	if ([sender selectedSegmentIndex] == EAT_MUM_CATEGORY) {
		self.countChartView.yData  = [NSArray arrayWithObject:self.countEatMumYAxisData];
		self.amountChartView.yData = [NSArray arrayWithObject:self.amountEatMumYAxisData];
		self.amountChartView.ySuffix= @"min";		
		self.amountChartView.hidden = false;		
	}else if ([sender selectedSegmentIndex] == EAT_MILK_CATEGORY) {
		self.countChartView.yData  = [NSArray arrayWithObject:self.countEatMilkYAxisData];
		self.amountChartView.yData = [NSArray arrayWithObject:self.amountEatMilkYAxisData];
		self.amountChartView.ySuffix= @"ml";		
		self.amountChartView.hidden = false;
	}else if ([sender selectedSegmentIndex] == DRINK_WATER_CATEGORY) {
		self.countChartView.yData  = [NSArray arrayWithObject:self.countDrinkWaterYAxisData];
		self.amountChartView.yData = [NSArray arrayWithObject:self.amountDrinkWaterYAxisData];
		self.amountChartView.ySuffix= @"ml";		
		self.amountChartView.hidden = false;
	}else if ([sender selectedSegmentIndex] == PEE_CATEGORY) {
		self.countChartView.yData  = [NSArray arrayWithObject:self.countPeeYAxisData];
		self.amountChartView.hidden = true;
	}else if ([sender selectedSegmentIndex] == SHIT_CATEGORY) {
		self.countChartView.yData  = [NSArray arrayWithObject:self.countShitYAxisData];
		self.amountChartView.hidden = true;
	}else {
		return;
	}
	[self.countChartView setNeedsDisplay];
	[self.amountChartView setNeedsDisplay];	
}

@end
